package com.bluedot.www.baiyetest.servlet.baiyetest.service.impl;

import com.bluedot.www.baiyetest.servlet.baiyetest.dao.IUserDao;

import com.bluedot.www.baiyetest.servlet.baiyetest.pojo.User;
import com.bluedot.www.baiyetest.servlet.baiyetest.service.ServiceTest;
import com.bluedot.www.framework.ibatis.mybaits.io.Resources;
import com.bluedot.www.framework.ibatis.mybaits.sqlsession.SqlSession;
import com.bluedot.www.framework.ibatis.mybaits.sqlsession.SqlSessionFactory;
import com.bluedot.www.framework.ibatis.mybaits.sqlsession.SqlSessionFactoryBuilder;
import com.bluedot.www.framework.mvc.annotation.MyService;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Jiutwo
 * @since 2021/8/5 15:04
 */
@MyService
public class ServiceTestImpl implements ServiceTest {
    @Override
    public User findById(Integer id) throws IOException {
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory;
        factory = builder.build(in);
        //3.使用工厂生产SqlSession对象
        SqlSession session = factory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IUserDao userDao = session.getMapper(IUserDao.class);
        //5.使用代理对象执行方法
//        List<User> users = userDao.findAll();
        User user = userDao.findById(String.valueOf(id));
        //userDao.insertUser(new User(1,"小花",new Date(1920,11,23),"男","江西"));
        //      userDao.deletebyId("49");
        //       userDao.update(new User(42,"小花",new Date(1920,11,23),"男","江西"));
//        System.out.println(user);
//        for(User u : users){
//            System.out.println(u);
//        }
        //6.释放资源
        session.close();
        in.close();



//        User user = new User();
//        user.setAddress("地址");
//        user.setId(1);
//        user.setSex("男");
        System.out.println("service---user="+user);
        return user;
    }
}
